{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc3849af",
   "metadata": {},
   "source": [
    "# numpy对数组按索引查询：3种\n",
    "- 基础索引\n",
    "- 神奇索引\n",
    "- 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fceb9e65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89ded71b",
   "metadata": {},
   "source": [
    "## 一维数组。和python的List一样，索引取值查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8dc87a7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one = np.arange(20)\n",
    "one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1fbb091b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56e06a8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 8 19\n"
     ]
    }
   ],
   "source": [
    "print(one[3],one[8],one[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bf5476fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one[2:4] "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7567171",
   "metadata": {},
   "source": [
    "#### 切片的方式。取前不取后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6865121d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two = np.arange(20).reshape(4,5)\n",
    "two"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb200d25",
   "metadata": {},
   "source": [
    "## 二维数组.A[行，列]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d9094e39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e3ba95a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b800c8c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[-1,2] # 最后一行，第2个数字"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c07dcb40",
   "metadata": {},
   "source": [
    "### 分别用行坐标，列坐标进行筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a79c51de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 11, 12, 13, 14])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "42588ad9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([15, 16, 17, 18, 19])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "120656af",
   "metadata": {},
   "source": [
    "### 只取行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "80e30ef5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7f40ada7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[1:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59ae0545",
   "metadata": {},
   "source": [
    "### 筛选多行，最后一行不取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "062b4f0b",
   "metadata": {},
   "source": [
    "### 筛选多行，筛选多列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3fc3af50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8a112a7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3],\n",
       "       [7, 8]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[:2,2:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa3b0478",
   "metadata": {},
   "source": [
    "# 切片的修改会修改原来的数组\n",
    "- 原因：numpy经常处理大数据，避免每次都要复制，所以直接批量修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1fa9c065",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bda656c0",
   "metadata": {},
   "source": [
    "#### 假如：更改第二，三行第二，三列的数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "bb59143f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0,   1,   2,   3,   4],\n",
       "       [  5, 666, 666,   8,   9],\n",
       "       [ 10, 666, 666,  13,  14],\n",
       "       [ 15,  16,  17,  18,  19]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[1:3,1:3] = 666\n",
    "two"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06d72fb6",
   "metadata": {},
   "source": [
    "# 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "64e65cc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one = np.arange(10)\n",
    "one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "aec53a9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False, False,  True,  True,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one>5 # 与数字进行比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0af0ed95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 7, 8, 9])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one[one>5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "467193af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two = np.arange(20).reshape(4,5)\n",
    "two"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "69e8b987",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True,  True,  True])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two[:,3]>5"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
